//
//  WYJStatusViewModel.swift
//  MySinaWeibo
//
//  Created by eastriver on 16/10/15.
//  Copyright © 2016年 believewilliam. All rights reserved.
//

import UIKit

class WYJStatusViewModel: NSObject {

    var avatarImg: UIImage?
    var memberImg: UIImage?
    var iconURL: URL?
    
    var reposts_text: String?
    var comments_text: String?
    var ohYeah_text: String?
    // 微博时间使用计算型属性
    var time_text: String {
        let dateString = statusModel!.created_at
        // 使用日期格式化器
//        let formatter = DateFormatter()
        // "Thu Sep 29 10:26:58 +0800 2016"
        sharedDateFormatter.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
//        formatter.locale = Locale(identifier: "en")
        let sinaDate = sharedDateFormatter.date(from: dateString!)
        // 防止转换不成功
        if sinaDate == nil {
            return ""
        }
        if idDateInThisYear(targetDate: sinaDate!) {
            // 今年
            let calendar = Calendar.current
            // 今天
            if calendar.isDateInToday(sinaDate!) {
                let currentDate = Date()
                
                let delta = currentDate.timeIntervalSince(sinaDate!)
                
                if delta < 60 {
                    // 1分钟之内
                    return "刚刚"
                } else if delta < 3600 {
                    // 1小时之内
                    let deltaMinute = Int(delta) / 60
                    return "\(deltaMinute)分钟前"
                } else {
                    let deltaHour = Int(delta) / 3600
                    return "\(deltaHour)小时前"
                }
            } else if calendar.isDateInYesterday(sinaDate!){
                // 昨天
                sharedDateFormatter.dateFormat = "昨天 HH:mm"
                return sharedDateFormatter.string(from: sinaDate!)
            } else {
                // 今年的其它天
                sharedDateFormatter.dateFormat = "MM-dd"
                return sharedDateFormatter.string(from: sinaDate!)

            }
        } else {
            // 今年以前
            sharedDateFormatter.dateFormat = "yyyy-MM-dd"
            return sharedDateFormatter.string(from: sinaDate!)
        }
        
    }
    // 微博来源
    var source_text: String?
    var pictureArray: [WYJStatusPictureModel]? {
        return statusModel?.retweeted_status == nil ? statusModel?.pic_urls : statusModel?.retweeted_status?.pic_urls
    }
    
    var statusModel: WYJStatusModel? {
        didSet {
            dealWithAvatarImg()
            dealWithMemberImg()
            dealWithIconURL()
            source_text = dealWithSource()
            reposts_text = dealToolBarText(count: statusModel?.reposts_count ?? 0, defaultText: "转发")
            comments_text = dealToolBarText(count: statusModel?.comments_count ?? 0, defaultText: "评论")
            ohYeah_text = dealToolBarText(count: statusModel?.reposts_count ?? 0, defaultText: "赞")
        }
    }
    // 处理工具栏文字
    func dealToolBarText(count: Int, defaultText: String) -> String {
        
        if count == 0 {
          return defaultText
        }
        if count > 10000 {
            return "\(Double(count / 1000) / 10)万"
        }
        return count.description
        
    }
    private func dealWithAvatarImg() {
        switch statusModel?.user?.verified_type ?? -1 {
        case 0:
            avatarImg = #imageLiteral(resourceName: "avatar_vip")
        case 2,3,5:
            avatarImg = #imageLiteral(resourceName: "avatar_enterprise_vip")
        case 220:
            avatarImg = #imageLiteral(resourceName: "avatar_grassroot")
        default:
            avatarImg = nil
        }
    }
    private func dealWithMemberImg() {
        if let mBrank = statusModel?.user?.mbrank, mBrank > 0 && mBrank < 7 {
            let imgName = "common_icon_membership_level\(mBrank)"
            memberImg = UIImage(named: imgName)
        }
    }
    private func dealWithIconURL() {
        let urlString = statusModel?.user?.avatar_large ?? ""
        iconURL = URL(string: urlString)
    }
    private func dealWithSource() -> String {
        // <a href=\"http://app.weibo.com/t/feed/3jskmg\" rel=\"nofollow\">iPhone 6s</a>
        let sourceString = self.statusModel?.source ?? ""
        let startFlag = "\">"
        let endFlag = "</a>"
        guard let startRange = sourceString.range(of: startFlag), let endRange = sourceString.range(of: endFlag) else {
            return "未知"
        }
        let startIndex = startRange.upperBound
        let endIndex = endRange.lowerBound
        let targetRange = startIndex..<endIndex
        let targetString = sourceString.substring(with: targetRange)
        return targetString
    }
    private func idDateInThisYear(targetDate: Date) -> Bool{
        // 使用日期格式化器
//        let formatter = DateFormatter()
        // "Thu Sep 29 10:26:58 +0800 2016"
        sharedDateFormatter.dateFormat = "yyyy"
        let targetYear = sharedDateFormatter.string(from:targetDate)
        // 获取当前时间的年
        let currentDate = Date()
        let currentYear = sharedDateFormatter.string(from: currentDate)
        return targetYear == currentYear

    }

}
